﻿﻿using System;
using System.Xml;
using StarTrek.Ship;
using StarTrek.World;

namespace StarTrek.Space
{
    class Star:iSpaceItem
    {
        private char Classification;
        private Quadrant parent;
        private Guid id;
        private string Name;
        private float Mass;
        private float EscapeVel;


        public Star(char SpectralClass):this()
        {
            Classification = SpectralClass;
        }
        public Star()
        {
            id = Guid.NewGuid();
        }
        public override string ToString()
        {
            string format = "{0} class Star, {1}";
            return string.Format(format, Classification, Name);
        }
        #region iSpaceItem Members
        public iSpaceItem FromXml(XmlNode val)
        {
            foreach (XmlNode value in val.ChildNodes)
            {
                switch (value.Name.ToUpper())
                {
                    case "CLASSIFICATION":
                        this.Classification = char.ToUpper(value.InnerText.ToCharArray()[0]);
                        break;
                    case "NAME":
                        this.Name = value.InnerText;
                        break;
                    case "MASS":
                        float.TryParse(value.InnerText, out this.Mass);
                        break;
                    case "ESCAPEVEL":
                        float.TryParse(value.InnerText, out this.EscapeVel);
                        break;
                    case "#COMMENT":
                        break;
                    default:
                        throw new NotImplementedException();
                }
            }
            return this;
        }
        public Quadrant Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }
        public bool isShipDocked()
        {
            return false; // ships cann't dock at this terrian
        }

        public iShip DockedShip
        {
            get
            {
                return null; //There is not a ship docked here
            }
            set
            {
                throw new ArgumentException("Can't dock a ship at a star");//Docking a ship here does nothing
            }
        }
        public iShip PresentShip
        {
            get
            {
                return null;
            }
            set
            {
                throw new ArgumentException("Can't fly a ship into a star");//Ship can't drive here.
            }
        }

        public SpaceUnitInfo BuildInfo()
        {
            throw new NotImplementedException();
        }

        public float CalculateDistance(iSpaceItem Target)
        {
            if (parent.Contains(Target))
                return parent.CalculateDistance(this, Target);
            else
                return Parent.Parent.CalculateDistance(this, Target);
        }

        public float TerrainPenalty
        {
            get { return EscapeVel;/* Star Gravity costs more*/ }
        }

        public Guid SectorIdentifier
        {
            get { return id; }
        }

        public bool isDockable
        {
            get { return false; }
        }

        public bool isTravelable
        {
            get { return false; }
        }
        #endregion
    }
}